package com.itheima.demogame;

import java.util.*;

public class Room {

    // 装牌容器
    private final List<Pocker> allPokers;
    private final Scanner sc = new Scanner(System.in);
    private final List<Pocker> lastPoker;

    // 初始化牌
    {
        allPokers = new ArrayList<Pocker>();
        lastPoker = new ArrayList<>();
        // 初始化54张扑克牌，其中花色为符号
        String[] colors = {"♥", "♦", "♣", "♠"};
        String[] sizes = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        // 先添加小王和大王
        Collections.addAll(allPokers, new Pocker("小王", "🃏"), new Pocker("大王", "🃏"));
        // 添加其他牌
        for (String color : colors) {
            for (String size : sizes) {
                allPokers.add(new Pocker(size, color));
            }
        }
        System.out.println("牌库为：" + allPokers);
    }

    public void startFourPlayer() {
        // 四人斗地主

        // 创建一个Map，用于存储玩家和牌的关系
        Map<String, List<Pocker>> players = new HashMap<>();
        players.put("张三", new ArrayList<>());
        players.put("李四", new ArrayList<>());
        players.put("王五", new ArrayList<>());
        players.put("赵六", new ArrayList<>());

        // 四人斗地主用两副牌，且底牌为8张
        List<Pocker> pokers = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            pokers.addAll(this.allPokers);
        }
        // 洗牌
        Collections.shuffle(pokers);
        System.out.println("洗牌后：" + pokers);

        // 发牌, 拿底牌
        lastPoker.addAll(pokers.subList(pokers.size() - 8, pokers.size()));
        for (int i = 0; i < pokers.size() - 8; i++) {
            Pocker pocker = pokers.get(i);
            if (i % 4 == 0) {
                // 张三
                players.get("张三").add(pocker);
            } else if (i % 4 == 1) {
                // 李四
                players.get("李四").add(pocker);
            } else if (i % 4 == 2) {
                // 王五
                players.get("王五").add(pocker);
            } else {
                // 赵六
                players.get("赵六").add(pocker);
            }
        }
        System.out.println("请输入抢地主的角色名字：");
        String name = sc.next();
        label:
        while (true) {
            switch (name) {
                case "张三":
                    players.get("张三").addAll(lastPoker);
                    break label;
                case "李四":
                    players.get("李四").addAll(lastPoker);
                    break label;
                case "赵六":
                    players.get("赵六").addAll(lastPoker);
                    break label;
                case "王五":
                    players.get("王五").addAll(lastPoker);
                    break label;
                default:
                    System.out.println("输入错误，请重新输入");
                    break;
            }
        }

        // 遍历玩家集合，并对玩家的牌进行排序
        for (Map.Entry<String, List<Pocker>> entry : players.entrySet()) {
            List<Pocker> sortedList = entry.getValue();
            Collections.sort(sortedList);
        }

        // 遍历玩家集合，打印玩家的牌和底牌
        System.out.println("底牌为：" + lastPoker);
        for (Map.Entry<String, List<Pocker>> entry : players.entrySet()) {
            System.out.println(entry.getKey() + "的牌为：" + entry.getValue());
        }

    }

    public void startTreePlayer() {
        // 三人斗地主
        // 洗牌
        List<Pocker> pokers = new ArrayList<>(this.allPokers);
        Collections.shuffle(pokers);

        // 定义三个玩家 每个玩家都有自己的容器
        Map<String, List<Pocker>> players = new HashMap<>();
        players.put("张三", new ArrayList<>());
        players.put("李四", new ArrayList<>());
        players.put("王五", new ArrayList<>());

        System.out.println("开始发牌");
        // 发牌
        lastPoker.addAll(pokers.subList(pokers.size() - 3, pokers.size()));
        for (int i = 0; i < pokers.size() - 3; i++) {
            Pocker pocker = pokers.get(i);
            if (i % 3 == 0) {
                // 张三
                players.get("张三").add(pocker);
            } else if (i % 3 == 1) {
                // 李四
                players.get("李四").add(pocker);
            } else {
                // 王五
                players.get("王五").add(pocker);
            }
        }

        // 拿底牌
        System.out.println("请输入抢地主的角色名字：");
        String name = sc.next();
        label:
        while (true) {
            switch (name) {
                case "张三":
                    players.get("张三").addAll(lastPoker);
                    break label;
                case "李四":
                    players.get("李四").addAll(lastPoker);
                    break label;
                case "王五":
                    players.get("王五").addAll(lastPoker);
                    break label;
                default:
                    System.out.println("输入错误，请重新输入");
                    break;
            }
        }
        // 遍历玩家集合，并对玩家的牌进行排序
        for (Map.Entry<String, List<Pocker>> entry : players.entrySet()) {
            List<Pocker> sortedList = entry.getValue();
            Collections.sort(sortedList);
        }

        // 遍历玩家集合，打印玩家的牌和底牌
        System.out.println("底牌为：" + lastPoker);
        for (Map.Entry<String, List<Pocker>> entry : players.entrySet()) {
            System.out.println(entry.getKey() + "的牌为：" + entry.getValue());
        }
    }
}
